package com.todo.member.application.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.component.redis.RedisService;
import com.todo.member.application.convert.MemberInfoConvert;
import com.todo.member.application.entity.bo.MemberDetailBO;
import com.todo.member.application.entity.bo.MemberInfoBO;
import com.todo.member.application.entity.condition.MemberInfoConditionBO;
import com.todo.member.application.service.MemberInfoService;
import com.todo.member.common.base.redis.MemberKeyBuild;
import com.todo.member.infrastructure.entity.domain.MemberInfoDO;
import com.todo.member.infrastructure.entity.po.MemberInfoPO;
import com.todo.member.infrastructure.repository.MemberInfoRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author azhebuxing
 * @date 2025/2/12 21:54
 * @description
 */
@Slf4j
@Service
public class MemberInfoServiceImpl implements MemberInfoService {

    @Resource
    private MemberInfoRepository memberInfoRepository;
    @Resource
    private RedisService redisService;


    @Override
    public Boolean insertMemberInfo(Long userId) {
        if (userId == null) {
            return false;
        }
        MemberDetailBO memberDetailBO = queryMemberDetailByUserId(userId);
        if (memberDetailBO == null || memberDetailBO.getMemberInfoBO() == null) {
            return true;
        }
        Boolean updated = memberInfoRepository.insertMemberInfo(userId);
        return updated;
    }

    @Override
    public Boolean updateMemberInfo(MemberInfoBO memberInfoBO) {
        if (memberInfoBO == null || memberInfoBO.getId() == null) {
            return false;
        }
        MemberInfoPO memberInfoPO = MemberInfoConvert.convertToMemberInfoPO(memberInfoBO);
        Boolean updated = memberInfoRepository.updateMemberInfo(memberInfoPO);
        if (updated) {
            redisService.del(MemberKeyBuild.buildMemberDetailKey(memberInfoBO.getUserId()));
        }
        return null;
    }

    @Override
    public Boolean removeMemberInfoById(Long id) {
        if (id == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        MemberInfoBO memberInfoBO = queryMemberInfoById(id);
        if (memberInfoBO == null) {
            return true;
        }
        Boolean updated = memberInfoRepository.removeMemberInfoByUserId(id);
        if (updated) {
            redisService.del(MemberKeyBuild.buildMemberDetailKey(memberInfoBO.getUserId()));
        }
        return updated;
    }

    @Override
    public MemberInfoBO queryMemberInfoById(Long id) {
        if (id == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        MemberInfoPO memberInfoPO = memberInfoRepository.queryMemberInfoPOById(id);
        return MemberInfoConvert.convertToMemberInfoBO(memberInfoPO);
    }

    @Override
    public PageResult<MemberInfoBO> queryMemberInfoByPage(MemberInfoConditionBO memberInfoConditionBO) {
        return null;
    }

    @Override
    public MemberInfoBO queryMemberInfoByUserId(Long userId) {
        if (userId == null) {
            return null;
        }
        MemberDetailBO memberDetailBO = queryMemberDetailByUserId(userId);
        if (memberDetailBO == null) {
            return null;
        }
        return memberDetailBO.getMemberInfoBO();
    }

    @Override
    public MemberDetailBO queryMemberDetailByUserId(Long userId) {
        if (userId == null) {
            return null;
        }
        if (redisService.hasKey(MemberKeyBuild.buildMemberDetailKey(userId))) {
            String cacheValue = redisService.get(MemberKeyBuild.buildMemberDetailKey(userId));
            MemberDetailBO memberDetailBO = JSONObject.parseObject(cacheValue, MemberDetailBO.class);
            return memberDetailBO;
        }
        MemberInfoDO memberInfoDO = memberInfoRepository.queryMemberInfoByUserId(userId);
        if (memberInfoDO == null) {
            return null;
        }
        MemberDetailBO memberDetailBO = MemberInfoConvert.convertToMemberDetailBO(memberInfoDO);
        redisService.set(MemberKeyBuild.buildMemberDetailKey(userId), JSONObject.toJSONString(memberDetailBO));
        return memberDetailBO;
    }
}
